<template>
  <div>
    <el-dialog title="检查项列表" :visible.sync="show" width="600px" :close-on-click-modal="false">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['manager:device:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['manager:device:remove']">删除</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="检查项名称" align="center" prop="name" />
        <el-table-column label="测试方法和要求" align="center" show-overflow-tooltip prop="specification" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-hasPermi="['manager:device:edit']" @click="handleEdit(scope.row)">
              修改
            </el-button>
            <el-button size="mini" type="text" v-hasPermi="['manager:device:remove']" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog :title="(itemForm.id ? '编辑' : '新增') + '检查项'" :visible.sync="editShow" width="600px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="itemForm" :model="itemForm" :rules="deviceRules" label-width="120px">
        <el-form-item label="检查项名称" prop="name">
          <el-input v-model="itemForm.name" placeholder="请输入名称" clearable maxlength="120" />
        </el-form-item>
        <el-form-item label="测试方法和要求" prop="specification">
          <el-input type="textarea" v-model="itemForm.specification" :autosize="{ minRows: 4 }" maxlength="3000" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submititemForm">保 存</el-button>
        <el-button @click="deviceCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listItem, postItem, updateItem, delItem } from "@/api/device/system.js";
export default {
  name: "Device",
  data() {
    return {
      loading: false, // 加载中
      itemForm: {
        id: null,
        name: null,
        deviceId: null,
      },
      ids: [], // 选中数组
      deviceRules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        specification: [
          { required: true, message: "测试方法和要求不能为空", trigger: "blur" },
        ],
      },
      multiple: true, // 非多个禁用
      editShow: false, // 检查项编辑弹窗
      dataList: [], //检查项页面详情列表
    };
  },
  props: {
    value: {
      type: Boolean,
      default: false,
      required: true,
    },
    deviceId: {
      type: [String, Number],
      default: "",
    },
  },
  computed: {
    show: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
  },
  watch: {
    value(val) {
      if (val) {
        this.getList();
      }
    },
  },
  methods: {
    // 取消检查项编辑
    deviceCancel() {
      this.editShow = false;
      this.resetForm("itemForm");
    },

    // 查询检查项列表
    getList() {
      this.loading = true;
      listItem(this.deviceId)
        .then((res) => {
          this.dataList = res.data;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    //新增检查项
    handleAdd() {
      this.itemForm = {
        id: null,
        name: null,
        deviceId: this.deviceId,
      };
      this.editShow = true;
    },

    // 编辑检查项
    handleEdit(row) {
      this.itemForm = Object.assign({}, row);
      this.editShow = true;
    },

    // 删除检查项
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除编号为'" + ids + "'的检查项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delItem(ids).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        })
        .catch(() => { });
    },

    // 提交检查项表单
    submititemForm() {
      this.$refs.itemForm.validate((valid) => {
        if (valid) {
          if (this.itemForm.id) {
            updateItem(this.itemForm).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.editShow = false;
              this.getList();
            });
          } else {
            postItem(this.itemForm).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.editShow = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
